<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    :close-on-click-modal="false"
    width="600px"
    @close="close"
  >
    <div v-loading="loading" element-loading-text="修改中">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="新密码：" prop="password">
          <el-input type="newPassword" style="display:none" />
          <el-input
            v-model.trim="form.password"
            placeholder="请输入（6~12个字符，支持数字，大小写字母）"
            type="password"
            show-password
            autocomplete="new-password"
          />
        </el-form-item>
        <el-form-item label="确认新密码：" prop="checkPass">
          <el-input type="password" style="display:none" />
          <el-input
            v-model.trim="form.checkPass"
            placeholder="请再次输入新密码"
            type="password"
            show-password
            autocomplete="new-password"
          />
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
      <el-button size="small" round :disabled="loading" type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { validEngNum } from '@/utils/validate'
import { resetSpPw } from '@/api/sp'

export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback(new Error('请输入密码'))
      } else if (!validEngNum(value, 6, 12)) {
        callback(new Error('长度为6~12个字符,支持数字,大小写字母'))
      } else if (!this.$isNull(this.form.checkPass)) {
        this.$refs.form.validateField('checkPass')
        callback()
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 校验字段相关
      rules: {
        password: [{ required: true, validator: validatePassword, trigger: ['blur', 'change'] }],
        checkPass: [{ required: true, validator: validatePassword2, trigger: ['blur', 'change'] }]
      },
      title: '重置服务商密码',
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: false,
      loading: false,
      // 数据
      form: {
        password: null,
        checkPass: null
      }
    }
  },
  computed: {
    ...mapGetters(['loginInfo'])
  },
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {

  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    // 弹框相关
    open() {
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    close() {
      this.form = {
        password: null,
        checkPass: null
      }
      this.$refs.form.resetFields()
      this.loading = false
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const params = Object.assign({}, this.form)
          delete params.checkPass
          params.servicerId = this.$route.query.spId
          params.userId = this.loginInfo.userInfo.id
          resetSpPw({ data: params })
            .then(() => {
              this.$notify({
                title: '成功',
                message: '重置成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.hide()
            }).finally(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }

    /**
     * 数据组
     */
    /**
     * 按钮组
     */
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
